
var pageContext = new PageContext(settings||{});   
(function ($) {
    "use strict"; 
jQuery(function($) {
	pageContext.init();
//	$("#user-profile-2 .nav li>a").on("click",function(){
//		var $a = $(this);
//		var target = $a.attr("target");
//		$a.closest("ul").find("li").each(function(){
//			$(this).removeClass("active");
//			$(this).find("a").attr("aria-expanded","false");
//			var target = $(this).find("a").attr("target");
//			$(target).removeClass("active");
//		})
//		$a.attr("aria-expanded","true");
//		$a.closest("li").addClass("active");
//		$(target).addClass("active");
//	})
//	$("#user-profile-2 .nav li:first>a").click();
	
	loadData();
	pageInit();
}); 
function editable(){
	$.fn.editable.defaults.emptytext="空值";
	$.fn.editable.defaults.ajaxOptions = {type: "POST"};
	$('#accountForm span[name]').each(function(){
		$(this).editable({
			type: 'text',
			name:$(this).attr("name"),
			pk: $('#accountForm').attr("val"),
			url: $('#accountForm').attr("action"),
			title:$(this).closest("div").prev("div").html(),
	        params: function(params) {
	            var newparams = {};
	            newparams[params.name] = params.value;
	            newparams["id"] = params.pk;
	            return newparams;
	        }
			,validate: function(value) {
	        }
		});
	});
}

function loadData(id){
	//editables on first profile page
	$.fn.editable.defaults.mode = 'inline';
	$.fn.editableform.loading = "<div class='editableform-loading'><i class='ace-icon fa fa-spinner fa-spin fa-2x light-blue'></i></div>";
    $.fn.editableform.buttons = '<button type="submit" class="btn btn-info editable-submit"><i class="ace-icon fa fa-check"></i></button>'+
                                '<button type="button" class="btn editable-cancel"><i class="ace-icon fa fa-times"></i></button>';    

	// *** editable avatar *** //
	try {//ie8 throws some harmless exceptions, so let's catch'em

		//first let's add a fake appendChild method for Image element for browsers that have a problem with this
		//because editable plugin calls appendChild, and it causes errors on IE at unpredicted points
		try {
			document.createElement('IMG').appendChild(document.createElement('B'));
		} catch(e) {
			Image.prototype.appendChild = function(el){}
		}

		var avatar = $('#avatar').editable({
			type: 'image',
			image: {
				//specify ace file input plugin's options here
				btn_choose: '头像',
				droppable: true,
				maxSize: 1100000,//~100Kb
				//and a few extra ones here
				name: 'avatar',//put the field name here as well, will be used inside the custom plugin
				on_error : function(error_type) {//on_error function will be called when the selected file has a problem
					if(error_type == 1) {//file format error
						alert("文件必须是图片");
					} else if(error_type == 2) {//file size rror
						alert("图片太大了！");
					}
					else {//other error
					}
				},
				on_success : function() {
				}
			}, 
			url: function(params) {
				// ***UPDATE AVATAR HERE*** //
				//for a working upload example you can replace the contents of this function with 
				//examples/profile-avatar-update.js
				var deferred = new $.Deferred

				var value = $('#avatar').next().find('input[type=hidden]:eq(0)').val();
				if(!value || value.length == 0) {
					deferred.resolve();
					return deferred.promise();
				}
				//if(confirm("确定保存？")){
				params.name=$(this).next().find(".ace-file-name").attr("data-title");
				console.log(params)
				 pageContext.confirm("确定保存？",function(){
					 params.path="account";
					 var thumb = null;
					 if("FileReader" in window) {
						//for browsers that have a thumbnail of selected image
						var thumb = $('#avatar').next().find('img').data('thumb');
						params.file=thumb;
					}
					params.flag = $("#source_flag_s").val();
				  $.ajax({
						url:window.global.ctx+"/sys/file/upload/base64",
						type:'POST', //GET
						async:false,    //或false,是否异步
						data:params,
						timeout:5000,    //超时时间
						dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
						success:function(data,textStatus,jqXHR){
							if(data.status!=200){
								deferred.resolve();
								alert(data.msg)
							}else{
								data.status='ok';
								deferred.resolve(data);
							}
						} 
					})
		        });
				return deferred.promise();
				// ***END OF UPDATE AVATAR HERE*** //
			},

			success: function(response, newValue) {
				$(".profile-picture button.editable-cancel").click();
				if(response){
					$("#avatar").attr("src",$("#imgUrl_s").attr("relativeurl")+""+response.data.id).removeClass("editable-open");
					$("#imgUrl_s").val(response.data.id);
				}
				//$(".editableform-loading").hide();
				//$(".editableform").show();
			}
		})
		$(".profile-picture").on("click","button.editable-cancel", function() {
			$("#avatar").attr("src",$("#imgUrl_s").attr("relativeurl")+$("#avatar").attr("oldval")).removeClass("editable-open");
			$("#imgUrl_s").val($("#imgUrl_s").attr("oldval"));
			//$(".editable-container").remove();
			//$(".profile-picture img").show();
			
		 });
		$(".profile-picture ").on("click","a.remove", function() {
			$("#avatar").attr("src",$("#imgUrl_s").attr("relativeurl")+$("#avatar").attr("oldval")).removeClass("editable-open");
			$("#imgUrl_s").val($("#imgUrl_s").attr("oldval"));
		 });
	}catch(e) {
		console.log(e)
	}

	/////////////////////////////////////
	$(document).one('ajaxloadstart.page', function(e) {
		//in ajax mode, remove remaining elements before leaving page
		try {
			$('.editable').editable('destroy');
		} catch(e) {}
	});
}
   
}(window.jQuery));